package org.example;

/*
 * @Description:
 * @Author: chenzhen
 * @Date: 2023/3/23 13:27
 */

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class EsMain {

    public static void main(String[] args) throws IOException {
        /** 测试地址  **/
        RestClientBuilder builder = RestClient.builder(
                        new HttpHost("es.rdc.finstone.com.cn"))
                .setRequestConfigCallback(
                        new RestClientBuilder.RequestConfigCallback() {
                            @Override
                            public RequestConfig.Builder customizeRequestConfig(
                                    RequestConfig.Builder requestConfigBuilder) {
                                return requestConfigBuilder
                                        .setConnectTimeout(5000)
                                        .setSocketTimeout(60000);
                            }
                        });
         //开始设置用户名和密码
       CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        String username = "";
        String password = "";
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        builder.setHttpClientConfigCallback(f-> f.setDefaultCredentialsProvider(credentialsProvider));

        RestHighLevelClient restClient = new RestHighLevelClient( builder);

/*
       创建索引不需要
        CreateIndexRequest request = new CreateIndexRequest("oa_log");
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
        );

        CreateIndexResponse createIndexResponse = restClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged());
*/


        /** 插入数据  **/
        SimpleDateFormat dfIndex = new SimpleDateFormat("yyyy-MM-dd");
        String dateIndex = dfIndex.format(new Date());

        IndexRequest requestOaLog = new IndexRequest("oa_log"+"-"+dateIndex);


        /** data是行数据 **/
        Map<String,Object> data = new HashMap<>();
        /** 这里id,name 只是例子，需要填充真实的数据项 **/
        data.put("idxx",4001);
        data.put("name","cz4001");

        /** 创建和更新时间 create_date update_date **/
        /** 所有日期需要按下面的日期格式化  **/
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        String date = df.format(new Date());
        data.put("create_date",date);
        data.put("update_date",date);
        requestOaLog.source(data);

        IndexResponse indexResponse = restClient.index(requestOaLog, RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());
        restClient.close();
    }
}
